//
// File:	DisplayManager.cpp
// Author:	Thomas TISSOT-DUPONT
// Login:	<thomas.tissotdupont@free.fr>
//
// Create:	26/01/2010 at 07:34:59
// Purpose:	
//
#include <QtGui/QLayout>
#include "Base.hpp"
#include "DisplayManager.hpp"
#include "BaseRenderable.hpp"
#include "Trace.hpp"

//------------------------------------------------------------------
// Constructor / Destructor
//------------------------------------------------------------------
DisplayManager::DisplayManager()
	:	m_pWindow(NULL),
		m_pRenderCanvas(NULL),
		m_renderTimer(),
		m_pView(new View())
{
}

DisplayManager::~DisplayManager()
{
	delete m_pWindow;
	delete m_pRenderCanvas;
}

//------------------------------------------------------------------
// Active ?
//------------------------------------------------------------------
bool DisplayManager::IsActive() const
{
	return m_pRenderCanvas->isVisible();
}

void DisplayManager::SetActive(bool bActive)
{
	m_pRenderCanvas->setVisible(bActive);
	if (bActive)
		m_pRenderCanvas->setFocus();
}

//------------------------------------------------------------------
// Render
//------------------------------------------------------------------
void DisplayManager::Render()
{
	if (!GlobalVariables::GuiManager::Get().IsVisible() && GlobalVariables::Engine::Get().IsInGame())
	{
		m_pRenderCanvas->repaint();
	}
	m_pWindow->Render();
}

//------------------------------------------------------------------
// Draw
//------------------------------------------------------------------
void DisplayManager::Draw(const IRenderable &renderable)
{
	const BaseRenderable *pBaseRenderable = dynamic_cast<const BaseRenderable *>(&renderable);
	if (pBaseRenderable != NULL)
	{
		const sf::Drawable *pDrawable = dynamic_cast<const sf::Drawable *>(pBaseRenderable);
		if (pDrawable != NULL)
			m_pRenderCanvas->Draw(*pDrawable);
	}
}

//------------------------------------------------------------------
// Start
//------------------------------------------------------------------
bool DisplayManager::Start()
{
	//Initialize the render canvas
	m_pRenderCanvas = new RenderCanvas(m_pWindow, QSize(m_pWindow->GetWidth(), m_pWindow->GetHeight()));
	m_pRenderCanvas->Init();
	if (m_pWindow->layout() != NULL)
		m_pWindow->layout()->addWidget(m_pRenderCanvas);

	m_renderTimer.setInterval(0);
	m_renderTimer.start();
	QObject::connect(&m_renderTimer, SIGNAL(timeout()), this, SLOT(Render()));
	QObject::connect(m_pWindow, SIGNAL(closed()), this, SLOT(onWindowClosed()));
	return true;
}

//------------------------------------------------------------------
// Stop
//------------------------------------------------------------------
bool DisplayManager::Stop()
{
	m_renderTimer.stop();
	delete m_pRenderCanvas;
	m_pRenderCanvas = NULL;
	return true;
}

//------------------------------------------------------------------
// Window
//------------------------------------------------------------------
IWindow *DisplayManager::GetWindow() const
{
	return m_pWindow;
}

void DisplayManager::InitWindow(const std::string &windowTitle, int iWidth, int iHeight, bool bFullScreen /* = false */)
{
	if (m_pWindow != NULL)
		DestroyWindow();
	m_pWindow = new Window(windowTitle, iWidth, iHeight, bFullScreen);
}

void DisplayManager::DestroyWindow()
{
	delete m_pWindow;
	m_pWindow = NULL;
}

void DisplayManager::onWindowClosed()
{
	GlobalVariables::Engine::Get().Stop();
}

//------------------------------------------------------------------
// Render Canvas
//------------------------------------------------------------------
RenderCanvas *DisplayManager::GetRenderCanvas() const
{
	return m_pRenderCanvas;
}

//------------------------------------------------------------------
// View
//------------------------------------------------------------------
IView *DisplayManager::GetView() const
{
	return m_pView;
}
